﻿using System;
using System.Collections.Generic;
using System.Data;
using TestSystem.DAL.News;

namespace TestSystem.BLL.News
{
    public class News : BusinessEntity
    {
        #region private fields
        private int _newsID;
        private int _facultyID;
        private string _newsName;
        private string _description;
        private string _shortDescription;
        private DateTime _publishingDate;
        private bool _isPublished;
        #endregion

        #region public props and methods
        public int NewsID
        {
            get { return _newsID; }
            set { _newsID = value; }
        }

        public int FacultyID
        {
            get { return _facultyID; }
            set { _facultyID = value; }
        }

        public string NewsName
        {
            get { return _newsName; }
            set { _newsName = value; }
        }

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        public string ShortDescription
        {
            get { return _shortDescription; }
            set { _shortDescription = value; }
        }

        public DateTime PublishingDate
        {
            get { return _publishingDate; }
            set { _publishingDate = value; }
        }

        public bool IsPublished
        {
            get { return _isPublished; }
            set { _isPublished = value; }
        }

        public override void Update()
        {
            NewsDataAccess.Update(this.NewsID, this.FacultyID, this.NewsName, this.Description, this.ShortDescription, this.PublishingDate, this.IsPublished);
            RemoveCacheItems("News_Faculty_" + FacultyID);
            RemoveCacheItems("News_All");
            RemoveCacheItems("News_University");
            RemoveCacheItems("News_AllCount");
        }

        protected override void Load(IDataReader rdr)
        {
            NewsID = (int) rdr["NewsID"];
            FacultyID = (int) rdr["FacultyID"];
            NewsName = NewsDataAccess.FromDBValue<string>(rdr["NewsName"]);
            Description = NewsDataAccess.FromDBValue<string>(rdr["Description"]);
            ShortDescription = NewsDataAccess.FromDBValue<string>(rdr["ShortDescription"]);
            PublishingDate = (DateTime) rdr["PublishingDate"];
            IsPublished = (bool) rdr["IsPublished"];
        }
        #endregion

        #region static methods 
        public static int Add(int facultyID, string newsName, string description, string shortDescription, DateTime publishingDate, bool isPublished)
        {
            var id = NewsDataAccess.Insert(facultyID, newsName, description, shortDescription, publishingDate, isPublished);
            RemoveCacheItems("News_Faculty_" + facultyID);
            RemoveCacheItems("News_All");
            RemoveCacheItems("News_University");
            RemoveCacheItems("News_AllCount");
            return id;
        }

        public static List<News> GetByFacultyID(int facultyID)
        {
            int count = 5;//top news for faculty
            List<News> facultyNews = null;
            string key = "News_Faculty_" + facultyID + "_" + count;
            if (Settings.NewsSettings.EnableCaching && Cache[key] != null)
                facultyNews = (List<News>) Cache[key];
            else
            {
                facultyNews = LoadList<News>(NewsDataAccess.GetNewsByFacultyID(facultyID, count));
                CacheData(key, facultyNews, -1, 3600);
            }
            return facultyNews;
        }

        public static List<News> GetForUniversity()
        {
            int newsPerOneFaculty = 2;//count of news per one faculty
            List<News> universityNews = null;
            string key = "News_University_" + newsPerOneFaculty;
            if (Settings.NewsSettings.EnableCaching && Cache[key] != null)
                universityNews = (List<News>) Cache[key];
            else
            {
                universityNews = LoadList<News>(NewsDataAccess.GetNewsForUniversity(newsPerOneFaculty));
                CacheData(key, universityNews, -1, 3600);
            }
            return universityNews;
        }

        public static List<News> GetAll(int startRowIndex, int maximumRows)
        {
            List<News> allNews = null;
            string key = "News_All_" + startRowIndex + "_" + maximumRows;
            var pageIndex = PageIndex(startRowIndex, maximumRows);
            var lowerBound = (pageIndex*maximumRows) + 1;
            var upperBound = (pageIndex + 1)*maximumRows;
            if(Settings.NewsSettings.EnableCaching && Cache[key] != null)
                allNews = (List<News>) Cache[key];
            else
            {
                allNews = LoadList<News>(NewsDataAccess.GetAllNews(lowerBound, upperBound));
                CacheData(key, allNews, -1, 3600);
            }
            return allNews;
        }

        public static List<News> GetAll(int pageIndex)
        {
            List<News> allNews = null;
            string key = "News_All_" + pageIndex;
            var maximumRows = Settings.NewsSettings.NewsPageSize;
            var lowerBound = (pageIndex * maximumRows) + 1;
            var upperBound = (pageIndex + 1) * maximumRows;
            if (Settings.NewsSettings.EnableCaching && Cache[key] != null)
                allNews = (List<News>)Cache[key];
            else
            {
                allNews = LoadList<News>(NewsDataAccess.GetAllNews(lowerBound, upperBound));
                CacheData(key, allNews, -1, 3600);
            }
            return allNews;
        }

        public static News GetByID(int newsID)
        {
            return LoadEntity<News>(NewsDataAccess.GetNewsByID(newsID));
        }

        public static void Remove(int newsID, int facultyID)
        {
            NewsDataAccess.Delete(newsID);
            RemoveCacheItems("News_Faculty_" + facultyID);
            RemoveCacheItems("News_All");
            RemoveCacheItems("News_University");
            RemoveCacheItems("News_AllCount");
        }

        public static int GetAllCount()
        {
            int count = 0;
            string key = "News_AllCount";
            if (Settings.NewsSettings.EnableCaching && Cache[key] != null)
                count = (int) Cache[key];
            else
            {
                count = NewsDataAccess.GetAllNewsCount();
                CacheData(key, count, -1, 3600);
            }
            return count;
        }
        #endregion
    }
}
